package gw.com.android.ui.gesture_pwd.base;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.List;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.GTConfig;
import gw.com.android.greendao.NotifyDataHelper;
import gw.com.android.model.DataManager;
import gw.com.android.presenter.LoginPresenter;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.WebFragment;
import gw.com.android.ui.gesture_pwd.GesturePwdResetActivity;
import gw.com.android.utils.CacheUtils;
import gw.com.android.utils.PackStatis;
import www.com.library.app.ObjectSessionStore;

public class GestureBaseActivity extends BaseActivity {
    protected Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected int getLayoutView() {
        return 0;
    }

    @Override
    protected void initLayoutView() {

    }

    @Override
    protected void initViewData() {

    }

    private String splitTag = "-";

    /**
     * 读取密码，并且输出String
     *
     * @param tag
     * @param pwd
     * @return
     */
    protected String showPwd(String tag, List<Integer> pwd) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < pwd.size(); i++) {//并且输出密码
            res.append(pwd.get(i) + splitTag);
        }
        Log.d(tag, "" + res.toString());
        return res.toString();
    }

    /**
     * 解析密码，并且输出List
     *
     * @param pwd
     * @return
     */
    protected List<Integer> parsePwdStr(String pwd) {
        List<Integer> res = new ArrayList<>();
        String[] s = pwd.trim().split(splitTag);
        for (int i = 0; i < s.length; i++) {
            if (TextUtils.isEmpty(s[i]))
                break;
            res.add(Integer.parseInt(s[i].trim()));
        }
        return res;
    }

    /**
     * 保存密码 到本地文件
     *
     * @param pwd
     */
    protected void savePwd(String pwd) {
        Log.d("gesturePwdTag", "即将把" + pwd + "存入到" + CacheUtils.get_CACHE_GESTURE_PWD_INT());
        CacheUtils.writeFile(CacheUtils.get_CACHE_GESTURE_PWD_INT(), pwd);
    }

    /**
     * 读取密码
     *
     * @return
     */
    protected String getPwd() {
        String res = CacheUtils.readFile(CacheUtils.get_CACHE_GESTURE_PWD_INT());
        Log.d("gesturePwdTag", "即将从" + CacheUtils.get_CACHE_GESTURE_PWD_INT() + "中，读取密码:" + res);
        return res;
    }

    /**
     * 文案的左右摇动动画
     */
    protected void animate(View tv_go) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_go, "translationX", -20, 20, -20, 0);
        objectAnimator.setDuration(300);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        objectAnimator.start();
    }

    /**
     * 如果解锁失败
     * 则强制退出登录，跳转至【登录页】，且清空密码
     */
    protected void onCheckFailed() {
        //强制退出登录怎么解
        forceExitLogin();
        GTConfig.instance().clearPassword(GTConfig.instance().mCurName);
        ActivityManager.showLoginActivity(this);
        finish();
    }

    private void forceExitLogin() {
        GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_GUEST);
        GTConfig.instance().mLastAccountType = GTConfig.ACC_TYPE_GUEST;
        GTConfig.instance().isPhoneLogin = false;
//					GTConfig.instance().phoneLoginInfo ="";
        GTConfig.instance().mCurLoginPhone = "";
        GTConfig.instance().mCurName = "";
        GTConfig.instance().mUserPwd = "";

        final MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
        if (null != mainActivity) {
            mainActivity.removeAllFragment(false, true);
        }
        WebFragment.clearCookie();
        DataManager.instance().reset();
        LoginPresenter presenter = new LoginPresenter(this);
        presenter.guestLogin();
        GTConfig.instance().setJpushAliasAndTags(null, "");
        PackStatis.getToService(PackStatis.EventAction.LOGOUT.getValue(), PackStatis.EventCategory.MAIN.getValue(), null, null);
        this.finish();
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (mainActivity != null) {
                    mainActivity.showMeFragment();
                }
                NotifyDataHelper.notifyChange();
            }
        });
    }
}
